class Product < ActiveRecord::Base
  validates_presence_of :title, :description, :image_url
  validates_numericality_of :price
  validates_uniqueness_of :title
  validates_format_of :image_url,
                      :with     => %r{^http:.+\.(gif|jpg|png)$}i,
                      :message  => "must be a valid URL for a GIF, JPG, or PNG image"
    
  # Return a lost of products that we can sell (which means they have to be available)
  # Show the most recently available items first
  def self.salable_items
    find(:all,
         :conditions  => "date_available <= now()",
         :order       => "date_available desc")
  end                      
  
  protected
  def validate
    errors.add(:price, "should be positive") unless price.nil? || price >= 0.01
  end  
end
